<div class="users index">
<h2 class="handle"><?php __('Users');?></h2>
<!-- Left Fat Section -->
<div class="widfats">
    <!-- Table Navigation -->
    <div class="tablenav">
        <div class="batchactions"></div>
        <?php echo $this->element('pager'); ?>
    </div>
    <!-- Table Content Area -->
    <table id="table_users" class="table fixed">
        <thead title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th width="5%" class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th width="12%"><?php echo $paginator->sort('id');?></th>
                <th width="24%"><?php echo $paginator->sort('username');?></th>
                <th width="24%"><?php echo $paginator->sort('group_id');?></th>
                <th width="35%" class="actions"><?php __('Actions');?></th>
            </tr>
        </thead>
        <tfoot title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th><?php echo $paginator->sort('id');?></th>
                <th><?php echo $paginator->sort('username');?></th>
                <th><?php echo $paginator->sort('group_id');?></th>
                <th class="actions"><?php __('Actions');?></th>
            </tr>
        </tfoot>
        <tbody>
            <?php foreach ($users as $i=>$user):
                $class = $i % 2 == 0 ? ' class="altrow"' : ''; ?>
            <tr<?php echo $class;?>>
                <td class="checkbox"><input type="checkbox" class="checkbox"/></td>
                <td><?php echo $user['User']['id']; ?></td>
                <td><?php echo $user['User']['username']; ?></td>
                <td><?php echo $html->link($user['Group']['name'], array('controller'=> 'groups', 'action'=>'view', $user['Group']['id'])); ?></td>
                <td class="actions">
                    <?php echo $html->link(__('View', true), array('action'=>'view', $user['User']['id'])); ?>
                    <?php echo $html->link(__('Edit', true), array('action'=>'edit', $user['User']['id'])); ?>
                    <?php echo $html->link(__('Delete', true), array('action'=>'delete', $user['User']['id'])); ?>
                    <?php echo $html->link(__('Security', true), array('action'=>'security', $user['User']['id'])); ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <!-- User Edit Form -->
    <div id="widget_edit_user" class="widget" style="margin-top: 20px">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Edit User');?></span></h3>
        <div class="content">
            <p class="faq"><?php __('There is no user to edit now'); ?></p>
        </div>
    </div>
</div>
<!-- Right sidebar section -->
<div class="sidebar">
    <!-- User Add Widget -->
    <div id="widget_add_user" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Add User');?></span></h3>
        <div class="content">
            <div class="users form">
                <?php e($form->create('User')); ?>
                    <fieldset>
                        <?php e($form->input('username', array(
                                                'label'=>__('User Name', true),
                                                'class'=>'input text',
                                                'autocomplete'=>'off'))); ?>
                        <?php e($form->input('group_id', array(
                                                'label'=>__('User Group', true),
                                                'empty' => __('-Select Group-', true),
                                                'type' => 'select',
                                                'class'=>'select'))); ?>
                        <?php e($form->input('password', array(
                                                'label'=>__('User Password', true),
                                                'value'=>'000000',
                                                'type'=>'password',
                                                'class'=>'input text'))); ?>
                        <?php e($form->input('usedefault', array(
                                                'label'=>__('Use default password(000000)', true),
                                                'type'=>'checkbox',
                                                'checked'=>'checked',
                                                'class'=>'checkbox'))); ?>
                        <?php e($form->hidden('active', array('value'=>'1', 'class'=>'hidden'))); ?><br class="clear"/><br class="clear"/>
                        <?php e($form->submit(__('Add User', true), array('class'=>'submit attention'))); ?>
                    </fieldset>
                <?php e($form->end()); ?>
            </div>
        </div>
    </div>
</div>
</div>
<script type="text/javascript">
jQuery(function(){
    /* justify and binding for UserAddForm */
    $('label[for=UserUsedefault]').addClass('checkbox');
    $('input#UserPassword').attr({'disabled': 'disabled', 'value': '000000'});
    // Change password field disable status of user check the UseDefault checkbox
    $('input#UserUsedefault').change(function(){
        if($(this).attr('checked')){
            $('input#UserPassword').attr({'disabled': 'disabled', 'value': '000000'}).blur();
        }else{
            $('input#UserPassword').removeAttr('disabled').focus().clearFields();
        }
    });
    // Validate UserAddForm
    $.sme.data.user = false;
    /*$('#UserAddForm').submit(function(){
        if(!$(this).valid()){ return false; }
        if(!$.sme.status.user){
            alert('该用户名已经存在, 不能添加');
            return false;
        }
        // We have to enable the password field, to send to server
        $('input#UserPassword').attr('disabled', false);
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'UserUsername',
                model: 'User',
                control: 'user',
                fielden: 'username',
                fieldzh: '用户名'
            });
        },
        rules: {
            'data[User][username]': 'required',
            'data[User][password]': 'required',
            'data[User][group_id]': 'required'
        },
        messages: {
            'data[User][username]': '用户名应该只包含数字和字母',
            'data[User][password]': '如果不使用默认密码, 请自行设置密码',
            'data[User][group_id]': '请为该用户选择1个用户组'
        }
    });
    $('#UserEditForm').submit(function(){
        if(!$(this).valid()){ return false; }
        if(!$.sme.status.user){
            alert('该用户名已经存在, 不能添加');
            return false;
        }
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'UserUsername',
                model: 'User',
                control: 'user',
                fielden: 'username',
                fieldzh: '用户名'
            });
        },
        rules: { 'data[User][username]': 'required' },
        messages: { 'data[User][username]': '用户名应该只包含数字和字母' }
    });*/
    // Bind User Edit/View/Delete callbacks
    $('table#table_users td.actions a[href*=edit]')
        .data('ajax', '#widget_edit_user div.content')
        .click($.sme.cbk.ajaxView);
    $('table#table_users td.actions a[href*=delete]')
        .click($.sme.cbk.ajaxDelete);
});
</script>